// wenshidu/wen_list/wen_list.js
let app = getApp()
let yuming = app.globalData.yuming
var shuzong = []
var shu = []
const request = require('../../config/request');
Page({
    ...request,
    /**
     * 页面的初始数据
     */
    data: {
        yuming: yuming,
        title: '设备列表',
        wenxia_gao: '',
        ws_list: [],
        wen_id: '', //温湿度id
        issb: false,
        page:1
    },
    // 查看更多
    lookmore() {
        this.setData({
            page: (this.data.page) + 1,
        })
        shuzong = this.data.ws_list
        this.getccsbList()
    },
    // 温湿度详情
    ws_xq(e) {
        var id = e.currentTarget.dataset.id
        var wenid = e.currentTarget.dataset.wenid
        var humid = e.currentTarget.dataset.humid
        var item = e.currentTarget.dataset.item
        var isxiu = e.currentTarget.dataset.isxiu
        wx.setStorageSync('shebeixq', item)
        console.log(humid)
        if (humid == '' || humid == null) {
            wx.showModal({
                title: '您还没有配置哦~',
                cancelText: '取消',
                confirmText: '前往',
                success: function (res) {
                    if (res.confirm) {
                        console.log("确定")
                        wx.navigateTo({
                            url: '/page_two/addjiance/addjiance?title=修改设备&state=1&she_id=' + id + "&isxiu=" + isxiu,
                        })
                    } else {

                        console.log("取消")
                    }
                }
            })
        } else {
            wx.navigateTo({
                url: '/wenshidu/wen_listxq/wen_listxq?she_id=' + id + "&wenid=" + wenid,
            })
        }

    },
    // 添加设备
    add_she() {
        wx.navigateTo({
            url: '/wenshidu/add_she/add_she',
        })
    },
    // 检测设备详情
    jiance_xq(e) {
        var id = e.currentTarget.dataset.id
        var item = e.currentTarget.dataset.item
        var isxiu = e.currentTarget.dataset.isxiu
        wx.setStorageSync('shebeixq', item)
        wx.navigateTo({
            url: '/page_two/addjiance/addjiance?title=修改设备&state=1&she_id=' + id + "&isxiu=" + isxiu,
        })
    },
    // 返回
    backs() {
        wx.navigateBack({
            delta: 0
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            heights: wx.getMenuButtonBoundingClientRect().top + 5,
            wenxia_gao: wx.getMenuButtonBoundingClientRect().top + 80,
            ding_gao: wx.getMenuButtonBoundingClientRect().top + 40,
        })
    },
    // 存储设备列表接口
    getccsbList() {
        wx.showLoading({
            title: '加载中',
        })
        let that = this
        this.myRequest(yuming + 'Yiyao/yiyaoDeviceList', {
            shopId: wx.getStorageSync('shopId'),
            page: this.data.page,
            keywords: ''
        }, "POST").then(res => {
            if (res.data.code == 200) {
                wx.hideLoading();
                var shu = res.data.data;
                console.log(shu)
                shu.forEach(item => {
                    if (item.humidity.length != 0) {
                        item.humidity.detail.time = this.formatDate(new Date(item.humidity.detail.time))
                    }
                })
                if (that.data.page != 1) {
                    if (shu.length != 10) {
                        that.setData({
                            issb: true,
                        })
                    }
                }
                this.setData({
                    ws_list: shuzong.concat(shu)
                })
            }
        })
    },
    formatTen: function (num) {
        return num > 9 ? (num + "") : ("0" + num);
    },
    formatDate: function (date) {
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var day = date.getDate();
        return year + "-" + this.formatTen(month) + "-" + this.formatTen(day);
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getccsbList()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },
})